<!--
 * @Descripttion: 报名情况
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-09-10 15:50:27
-->
<template>
  <div>
    <top-nav :isBack="true"></top-nav>
    <div class="page-main">
      <div class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="签到状态" prop="is_sign_in">
            <el-select
              @change="toResetForm"
              v-model="searchForm.is_sign_in"
              placeholder="请选择"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="报名时间" prop="range">
            <el-date-picker
              @change="toResetForm"
              v-model="searchForm.range"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              value-format="timestamp"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="page-search-form" style="padding-bottom: 24px">
        <el-row>
          <el-col :span="18" class="text-bold flex-y-center">数据列表</el-col>
          <el-col :span="6" class="text-right">
            <lb-button
              plain
              type="primary"
              icon="el-icon-download"
              :loading="downloadLoading"
              @click="toExportStaff"
              >{{ $t('action.export') }}</lb-button
            >
          </el-col>
        </el-row>
        <div class="space-lg"></div>
        <el-table
          ref="multipleTable"
          v-loading="loading"
          :data="tableData"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column
            v-for="(item, index) in tableHeader"
            :key="index"
            :label="item.label"
          >
            <template slot-scope="scope">
              <span v-if="item.type === 'txt'">{{ scope.row[item.prop] }}</span>
              <lb-image
                :src="scope.row[item.prop]"
                v-else-if="item.type === 'img'"
              ></lb-image>
              <div v-else>
                <p>{{ scope.row[item.prop] | handleTime(1) }}</p>
                <p>{{ scope.row[item.prop] | handleTime(2) }}</p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <lb-page
          :isShowBatch="false"
          :page="searchForm.page"
          :pageSize="searchForm.limit"
          :total="total"
          :selected="multipleSelection.length"
          @handleSizeChange="handleSizeChange"
          @handleCurrentChange="handleCurrentChange"
        >
        </lb-page>
      </div>
    </div>
  </div>
</template>

<script>
import pubic from '@/utils/public'
export default {
  data () {
    return {
      statusOptions: [
        {
          label: '全部',
          value: 3
        },
        {
          label: '已签到',
          value: 1
        },
        {
          label: '未签到',
          value: 0
        }
      ],
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > Date.now()
        }
      },
      searchForm: {
        is_sign_in: 3,
        range: '',
        id: 0,
        page: 1,
        limit: 10,
        page_count: 10
      },
      downloadLoading: false,
      multipleSelection: [],
      loading: false,
      tableHeader: [],
      tableData: [],
      total: 0
    }
  },
  created () {
    let { id } = this.$route.query
    this.searchForm.id = id
    this.getTableHeader()
    this.getTableDataList()
  },
  methods: {
    toResetForm () {
      this.$refs.multipleTable.clearSelection()
    },
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    /**
       * @method 导出
       */
    toExportStaff () {
      this.downloadLoading = true
      let { id: activityId, is_sign_in: sign, range } = JSON.parse(JSON.stringify(this.searchForm))
      let multipleSelection = JSON.parse(JSON.stringify(this.multipleSelection))
      let searchForm = {
        activity_id: activityId,
        id: [],
        start_time: '',
        end_time: '',
        is_sign_in: sign
      }
      if (typeof (range) === 'object' && range && range.length > 0) {
        searchForm.start_time = range[0] / 1000
        searchForm.end_time = range[1] / 1000 + 24 * 3600 - 1
      }
      if (typeof (multipleSelection) === 'object' && multipleSelection && multipleSelection.length > 0) {
        multipleSelection.map(item => {
          searchForm.id.push(item.id)
        })
      }
      let url = pubic.getProCurrentHref()
      let keywords = url.indexOf('?') > 0 ? '' : '?'
      let flag = url.indexOf('?') > 0
      Object.getOwnPropertyNames(searchForm).forEach((keys, value) => {
        keywords += flag ? `&${keys}=${searchForm[keys]}` : `${keys}=${searchForm[keys]}`
        flag = true
      })
      let token = sessionStorage.getItem('minitk')
      let dwonlaodUrl = `${url}/activity/admin/activityUserRecordAdmin/userRecordExcel${keywords}&token=${token}`
      window.location.href = dwonlaodUrl
      setTimeout(() => {
        this.downloadLoading = false
      }, 5000)
    },
    getTableHeader () {
      this.$api
        .getActivitySignUpHeader({ id: this.searchForm.id })
        .then(res => {
          if (res.code === 200) {
            console.log('s', res.data.data)
            this.tableHeader = res.data.data
          }
        })
    },
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let { searchForm } = this
      searchForm.page_count = searchForm.limit
      let isChange = typeof (searchForm.range) === 'object' && searchForm.range && searchForm.range.length > 0
      searchForm.start_time = isChange ? searchForm.range[0] / 1000 : ''
      searchForm.end_time = isChange ? searchForm.range[1] / 1000 : ''
      this.$api.getActivitySignUpList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          let data = res.data.data
          data.map(item => {
            item.check = false
          })
          this.tableData = data
          this.total = res.data.total
        }
      })
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
